﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace WindowsSpeech
{
    class Program
    {
        static StringBuilder builder = new StringBuilder();
        static string filename = System.AppDomain.CurrentDomain.BaseDirectory + "\\English.txt";
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                builder = new StringBuilder();
                builder.AppendLine("姓名：罗敏贵  ");
                builder.AppendLine("电话：13026209315 ");
                builder.AppendLine("邮箱：minguiluo@163.com ");
                builder.AppendLine("博客：http://www.cnblogs.com/luomingui ");
                builder.AppendLine("              ");
                Console.WriteLine(builder.ToString());

                builder = new StringBuilder();
                builder.AppendLine("文本转wav音频文件 ");
                builder.AppendLine("*************************************");
                builder.AppendLine("1: 一行一个生成wav音频文件 ");
                builder.AppendLine("2：把English文件里的内容生成一个wav音频文件 ");
                builder.AppendLine("3：退出 ");
                builder.AppendLine("              ");
                builder.AppendLine(" 请输入对应的数字执行相关的程序");
                builder.AppendLine("*************************************");
                if (!File.Exists(filename))
                {
                    File.CreateText(filename);
                    Console.WriteLine("English.txt文件不存在，系统已为你创建，请打开文本文件进行输入，在重新打开本工具。");
                }
                else
                {
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);
                    string lineToEnd = read.ReadToEnd();
                    read.Dispose();
                    if (!string.IsNullOrEmpty(lineToEnd))
                    {
                        ConsoleWrite();
                    }
                    else
                    {
                        Console.WriteLine("English不能为空.");
                    }
                }
              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
               
            }
            Console.ReadLine();
        }

        public static void ConsoleWrite()
        {
            Console.WriteLine(builder.ToString());
            Console.Write("请输入:");
            String temp = Console.ReadLine();
            while (!IsNum(temp))
            {
                Console.WriteLine("非数字,请重新输入。");
                temp = Console.ReadLine();
            }
            Execute(int.Parse(temp));
        }
        private static bool IsNum(string s)
        {
            return Regex.IsMatch(s, @"^\d+");
        }
        public static bool Execute(int number)
        {
            bool flg = true;
            switch (number)
            {
                case 1:
                    ReadLine();
                    break;
                case 2:
                    ReadToEnd();
                    break;
                case 3:
                    Environment.Exit(0);
                    break;
                default:
                    flg = false;
                    Console.Write("系统没有找到可以执行的命令。\t\n");
                    break;
            }
            Console.WriteLine("      ");
            ConsoleWrite();
            return flg;
        }
        public static void ReadLine()
        {
            Console.WriteLine("一行一个wav音频文件生成开始......");
            #region MyRegion
            System.IO.StreamReader read = new System.IO.StreamReader(filename);
            while (true)
            {
                string currentLine = read.ReadLine();
                if (string.IsNullOrEmpty(currentLine))
                {
                    break;
                }
                Thread.Sleep(1000);
                Console.WriteLine("---------------" + currentLine + "----------------");
                MySpeech myspeech = new MySpeech(new VoiceClass
                {
                    VoiceName = "Microsoft Sam",
                    Rate = -10,
                    Volume = 100,
                    VoiceText = currentLine
                });
                myspeech.SaveMp3();
            }
            read.Dispose();
            Console.WriteLine("生成一行一个wav音频文件完成.");
            #endregion
        }
        public static void ReadToEnd()
        {
            Console.WriteLine("全部行生成一个wav音频文件开始......");
            System.IO.StreamReader read = new System.IO.StreamReader(filename);
            string lineToEnd = read.ReadToEnd();
            read.Dispose();
            MySpeech myspeech = new MySpeech(new VoiceClass
            {
                VoiceName = "Microsoft Sam",
                Rate = -10,
                Volume = 100,
                VoiceText = lineToEnd
            });
            myspeech.SaveMp3();

            Console.WriteLine("生成wav音频文件完成.");
        }
    }
}
